package com.lexue.articlesync.repository;

import com.lexue.articlesync.entity.Article;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 文章数据访问接口
 */
@Repository
public interface ArticleRepository extends JpaRepository<Article, Long> {
    
    /**
     * 根据状态查找文章
     */
    List<Article> findByStatus(Article.ArticleStatus status);
    
    /**
     * 根据标题模糊查询
     */
    List<Article> findByTitleContainingIgnoreCase(String title);
    
    /**
     * 根据标签查询
     */
    @Query("SELECT a FROM Article a WHERE a.tags LIKE %:tag%")
    List<Article> findByTagsContaining(@Param("tag") String tag);
    
    /**
     * 查询定时发布的文章
     */
    @Query("SELECT a FROM Article a WHERE a.status = 'SCHEDULED' AND a.scheduledTime <= :now")
    List<Article> findScheduledArticles(@Param("now") LocalDateTime now);
    
    /**
     * 根据创建时间范围查询
     */
    List<Article> findByCreatedAtBetween(LocalDateTime startTime, LocalDateTime endTime);
    
    /**
     * 查询最近的文章
     */
    List<Article> findTop10ByOrderByCreatedAtDesc();
}